#!/bin/sh
#
# Run a simple backup using the compressed option
#   then backup four times, each with incremental then
#   do a bscan and restore.
#   It should require at least 4 different bsrs.
#

TestName="bscan-tape"
JobName=bscantape
. scripts/functions

require_tape_drive

copy_tape_confs

change_jobname NightlySave $JobName


# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

echo "${BackupDirectory}" >${tmp}/file-list

cp ${conf}/susan-sd.conf tmp/1
sed "s%# Maximum File Size%  Maximum File Size%" tmp/1 >${conf}/susan-sd.conf

start_test

cat <<END_OF_DATA >tmp/bconcmds
@$out tmp/log1.out
setdebug level=2 storage=tape
label storage=tape volume=TestVolume001 slot=0 pool=Default
run job=$JobName level=Full yes
wait
run job=$JobName level=Full yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=tape

echo "Backup 1 done"
# make some files for the incremental to pick up
echo "test test" > ${BackupDirectory}/additional2.txt

#
# run a second job
#
cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log1.out
run job=$JobName level=Incremental yes
wait
messages
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=tape

echo "Backup 2 done"
echo "test test" > ${BackupDirectory}/additional3.txt

#
# run a third job
#
run_bconsole
scripts/check_for_zombie_jobs storage=tape

echo "Backup 3 done"
# make some files for the incremental to pick up
echo "test test" > ${BackupDirectory}/additional4.txt

#
# run a fourth job
#
run_bconsole
scripts/check_for_zombie_jobs storage=tape

stop_susan

echo "Backup 4 done"
#
# now drop and recreate the database
#
${scripts}/drop_susan_tables >/dev/null 2>&1
${scripts}/make_susan_tables >/dev/null 2>&1
${scripts}/grant_susan_privileges >/dev/null 2>&1

echo "volume=TestVolume001" >tmp/bscan.bsr

bscan_libdbi

# If the database has a password pass it to bscan
if test "x${db_password}" = "x"; then
  PASSWD=
else
  PASSWD="-P ${db_password}"
fi

BSCAN_OUT="write_stdin_to_file ${tmp}/log-bscan.out"
if is_debug; then
   BSCAN_OUT="tee ${tmp}/log-bscan.out"
fi

$bin/bscan -c ${conf} $BSCANLIBDBI -B ${DBTYPE} -n ${db_name} -u ${db_user} $PASSWD -m -s -v -b $tmp/bscan.bsr tape 2>&1 | $BSCAN_OUT



cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log2.out
@#
@# now do a restore
@#
restore where=${cwd}/tmp/susan-restores select all storage=tape done
yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=tape
stop_susan

check_two_logs
check_restore_diff ${BackupDirectory}
end_test

